package it.mp.acquaalta.utility;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;

import android.content.Context;
import android.os.Build;

public class DecodificaPagina implements Serializable {
	private static final long serialVersionUID = -284815868527011643L;
	public static final String TAG = "AcquaAltaService";
	private static final String URL = "http://www.comune.venezia.it/flex/cm/pages/ServeBLOB.php/L/IT/IDPagina/2104";
	private static final String URL_EMULATOR = "http://www.comune.venezia.it/flex/cm/pages/ServeBLOB.php/L/IT/IDPagina/2104";

	private AcquaAltaBean aa;
	private static final String DELIMITATORE_DATA = "<th style='padding: .3em; border-bottom:1px solid #000; border-right:1px solid #000; background-color: #eeeeee; text-align: left;'>";
	private static final String DELIMITATORE_TIPO = "<td style='padding: .3em; border-bottom:1px solid #000; border-right:1px solid #000;'>";
	private static final String DELIMITATORE_VALORE = "<td style='padding: .3em; border-bottom:1px solid #000; border-right:1px solid #000;'>";

	public DecodificaPagina(Context context) throws Exception {
		URL url = null;
		if (!isDevice()){
			url = new URL(URL);
		}else{
			url = new URL(URL_EMULATOR);
		}
		if (url != null) {
			parseISToString(url.openStream());
		}
	}
	private final static boolean isDevice() {
		return !"sdk".equals(Build.MODEL) && !"sdk".equals(Build.PRODUCT) && !"generic".equals(Build.DEVICE);
	}

	
	private void parseISToString(InputStream is) {
		aa = new AcquaAltaBean();
		String line = null;
		String data;
		String tipo;
		String valore;
		int indexOf = 0;
		aa.alBean = new ArrayList<AcquaAltaBean.Bean>();
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(is, "ISO-8859-15"), 6000);
			while ((line = reader.readLine()) != null) {
				// System.out.println(line);
				indexOf = line.indexOf(DELIMITATORE_DATA);
				if (indexOf != -1) {
					indexOf += DELIMITATORE_DATA.length();
					data = line.substring(indexOf, line.indexOf("</th>"));
					line = reader.readLine();
					indexOf = line.indexOf(DELIMITATORE_TIPO) + DELIMITATORE_TIPO.length();
					tipo = line.substring(indexOf, line.indexOf("</td>"));
					line = reader.readLine();
					indexOf = line.indexOf(DELIMITATORE_VALORE) + DELIMITATORE_VALORE.length();
					valore = line.substring(indexOf, line.indexOf("</td>"));
					aa.alBean.add(aa.new Bean(data, tipo, valore));
				}
				if (aa.alBean.size() >= 12) {
					break;
				}
			}
			Calendar cal = Calendar.getInstance();
			StringBuffer sb = new StringBuffer();
			if (cal.get(Calendar.HOUR_OF_DAY) < 10) {
				sb.append("0");
			}
			sb.append(cal.get(Calendar.HOUR_OF_DAY));

			sb.append(":");
			if (cal.get(Calendar.MINUTE) < 10) {
				sb.append("0");
			}
			sb.append(cal.get(Calendar.MINUTE));

			sb.append(" ");
			if (cal.get(Calendar.MONTH) < 10) {
				sb.append("0");
			}
			sb.append(cal.get(Calendar.MONTH));

			sb.append("/");
			if (cal.get(Calendar.DAY_OF_MONTH) < 10) {
				sb.append("0");
			}
			sb.append(cal.get(Calendar.DAY_OF_MONTH));

			aa.dataUltimoAggiornamento = sb.toString();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (Exception ex) {
			}
		}
	}

	public AcquaAltaBean getBean() {
		return aa;
	}

}
